package com.fbs.chicken.shirodemo.exception;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 全局异常处理
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandlerAdvice {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private void log(Exception e) {
	    logger.error(e.getMessage(), e);
    }
	/**
	 * 鉴权失败
	 * @return
	 */
	@ExceptionHandler(AuthorizationException.class)
	@ResponseStatus(value=HttpStatus.FORBIDDEN)
	public String authorizationException(AuthorizationException e) {
        log(e);
		return e.getMessage();
	}

	/**
	 * 授权失败
	 */
	@ExceptionHandler(AuthenticationException.class)
	@ResponseStatus(value=HttpStatus.FORBIDDEN)
	public String authenticationException(AuthenticationException e) {
		log(e);
		return e.getMessage();
	}
}
